package org.wlw.servlet;

import org.wlw.factoy.SimpleFactory;
import org.wlw.models.Movie;
import org.wlw.service.IMovieService;
import org.wlw.utils.Contrants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/movie/add")
public class MovieAddServlet extends HttpServlet {
    private IMovieService movieService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(Contrants.JSP_PREFIX+"/movie/add.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //完成添加
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String director = req.getParameter("director");
        String actor = req.getParameter("actor");
        double score = Double.parseDouble(req.getParameter("score"));
        movieService = SimpleFactory.getMovieService();
        Movie movie = new Movie();
        movie.setActor(actor);
        movie.setDirector(director);
        movie.setName(name);
        movie.setScore(score);
        movie.setType(type);
        movieService.add(movie);
        resp.sendRedirect(req.getContextPath()+"/movies");
    }
}
